package hash;

import org.junit.Test;

import java.util.HashSet;

/*
 *@description 快乐数
 *@author hkfan
 *@version 1.0
 *@create 2025/6/4 16:54
 */
public class LeetCode_202 {


    public boolean isHappy(int n) {
        HashSet<Integer> set = new HashSet<>();
        int num = n;
        while (!set.contains(num)){
            set.add(num);
            num = calHappy(num);
            if (num==1){
                return true;
            }
        }
        return false;
    }

    private int calHappy(int num) {
        int sum=0;
        while (num!=0){
            sum+=Math.pow(num%10,2);
            num/=10;
        }
        return sum;
    }


}
